﻿Imports System.IO
Imports System.Net

Public Class FTP
    Shared Sub SendFTP(ByRef rp As DotNetResponse.SQLPersistence, ByVal strRutaOrigen As String, ByVal strRutaFtp As String, ByVal cnn As String)

        'Dim objFile As New FileInfo(strRutaOrigen)
        'My.Computer.Network.UploadFile(strRutaOrigen, strRutaFtp & objFile.Name, DotNetDAL.Generica.GetSQLConfig("ftp_usuario", cnn), DotNetDAL.Generica.GetSQLConfig("ftp_clave", cnn))







    End Sub
 
    Shared Sub GetFTPList(ByRef rp As DotNetResponse.SQLPersistence, ByRef objArrayListArchivos As ArrayList, ByVal cnn As String)
        Try
            rp.MensajeError = rp.MensajeError & "<br>" & "GetFtpList"
            Dim strDirFtp As FtpWebRequest = CType(FtpWebRequest.Create(DotNetDAL.Generica.GetSQLConfig("ftp_ruta_out", cnn)), FtpWebRequest)
            Dim objNC As New NetworkCredential(DotNetDAL.Generica.GetSQLConfig("ftp_usuario", cnn), DotNetDAL.Generica.GetSQLConfig("ftp_clave", cnn))
            strDirFtp.Credentials = objNC
            strDirFtp.Method = WebRequestMethods.Ftp.ListDirectory
            Dim objStreamReaderArchivos As New StreamReader(strDirFtp.GetResponse().GetResponseStream())
            Dim i As Integer

            rp.MensajeError = rp.MensajeError & "<br>" & "ArrayList de archivo "
            Do While i <> -1
                Try
                    objArrayListArchivos.Add(objStreamReaderArchivos.ReadLine())
                    i = objStreamReaderArchivos.Peek()
                Catch ex As Exception
                    i = -1
                    objArrayListArchivos = Nothing
                    rp.MensajeError = rp.MensajeError & "<br>" & "Fin ArrayList de archivo " & i & "__" & ex.Message
                End Try
            Loop

            objStreamReaderArchivos.Close()

            'OJO
            rp.MensajeError = rp.MensajeError & "<br>" & "Fin GetFtpList"

        Catch ex As Exception
            rp.Errores = True
            rp.MensajeError = rp.MensajeError & "<br>" & ex.Message
        End Try
    End Sub


    Shared Sub DownloadFTP(ByRef rp As DotNetResponse.SQLPersistence, ByVal strArchivo As String, ByVal strRuta As String, ByVal cnn As String)
        Try
            rp.MensajeError = rp.MensajeError & "<br>" & "Download FTP"
            

            If IO.File.Exists(strRuta & strArchivo) Then
                IO.File.Delete(strRuta & strArchivo)
            End If
            My.Computer.Network.DownloadFile(DotNetDAL.Generica.GetSQLConfig("ftp_ruta_out", cnn) & strArchivo, strRuta & strArchivo, DotNetDAL.Generica.GetSQLConfig("ftp_usuario", cnn), DotNetDAL.Generica.GetSQLConfig("ftp_clave", cnn))

            rp.MensajeError = rp.MensajeError & "<br>" & "Fin DownloadFTP"


        Catch ex As Exception
            rp.MensajeError = rp.MensajeError & "<br>" & ex.Message
        End Try
    End Sub
    Shared Sub DeleteFtp(ByRef rp As DotNetResponse.SQLPersistence, ByVal strArchivo As String, ByVal cnn As String)
        Try
            Dim strDirFtp As FtpWebRequest = CType(FtpWebRequest.Create(DotNetDAL.Generica.GetSQLConfig("ftp_ruta_out", cnn) & strArchivo), FtpWebRequest)
            Dim objNC As New NetworkCredential(DotNetDAL.Generica.GetSQLConfig("ftp_usuario", cnn), DotNetDAL.Generica.GetSQLConfig("ftp_clave", cnn))
            strDirFtp.Credentials = objNC
            strDirFtp.Method = WebRequestMethods.Ftp.DeleteFile
            strDirFtp.GetResponse()
        Catch ex As Exception
            rp.Errores = True
            rp.MensajeError = ex.Message
        End Try
    End Sub


    Shared Sub SendFTP(ByRef rp As DotNetResponse.SQLPersistence, ByVal strRutaOrigen As String, ByVal strRutaFtp As String, ByVal strFileFtp As String, ByVal ftp_clave As String, ByVal ftp_usuario As String)

        'Try

        '    Dim fl As New System.Net.WebClient
        '    fl.Credentials = New System.Net.NetworkCredential(ftp_usuario, ftp_clave)

        '    fl.UploadFile(strRutaFtp & strFileFtp, strRutaOrigen)

        '    Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create(strRutaFtp & strFileFtp), System.Net.FtpWebRequest)
        '    clsRequest.Credentials = New System.Net.NetworkCredential(ftp_usuario, ftp_clave)
        '    clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile

        '    ' read in file...
        '    Dim bFile() As Byte = System.IO.File.ReadAllBytes(strRutaOrigen)

        '    ' upload file...
        '    Dim clsStream As System.IO.Stream = _
        '        clsRequest.GetRequestStream()
        '    clsStream.Write(bFile, 0, bFile.Length)
        '    clsStream.Close()
        '    clsStream.Dispose()


        'Catch ex As Exception
        '    rp.Errores = True
        '    rp.MensajeError = ex.Message
        'End Try
        Try

            Dim fileInf As New FileInfo(strRutaOrigen)
            Dim uri As String = strRutaFtp & fileInf.Name
            Dim reqFTP As FtpWebRequest

            ' Create FtpWebRequest object from the Uri provided
            reqFTP = DirectCast(FtpWebRequest.Create(New Uri(strRutaFtp & "/" & strFileFtp)), FtpWebRequest)

            ' Provide the WebPermission Credintials
            reqFTP.Credentials = New NetworkCredential(ftp_usuario, ftp_clave)

            ' By default KeepAlive is true, where the control connection is not closed
            ' after a command is executed.
            reqFTP.KeepAlive = False

            ' Specify the command to be executed.
            reqFTP.Method = WebRequestMethods.Ftp.UploadFile

            ' Specify the data transfer type.
            reqFTP.UseBinary = True

            ' Notify the server about the size of the uploaded file
            reqFTP.ContentLength = fileInf.Length

            ' The buffer size is set to 2kb
            Dim buffLength As Integer = 2048
            Dim buff As Byte() = New Byte(buffLength - 1) {}
            Dim contentLen As Integer

            ' Opens a file stream (System.IO.FileStream) to read the file to be uploaded
            Dim fs As FileStream = fileInf.OpenRead()


            ' Stream to which the file to be upload is written
            Dim strm As Stream = reqFTP.GetRequestStream()

            ' Read from the file stream 2kb at a time
            contentLen = fs.Read(buff, 0, buffLength)

            ' Till Stream content ends
            While contentLen <> 0
                ' Write Content from the file stream to the FTP Upload Stream
                strm.Write(buff, 0, contentLen)
                contentLen = fs.Read(buff, 0, buffLength)
            End While

            ' Close the file stream and the Request Stream
            strm.Close()
            fs.Close()
        Catch ex As Exception
            rp.Errores = True
            rp.MensajeError = ex.Message
        End Try


    End Sub
    Shared Sub DeleteFtp(ByVal strRutaOrigen As String, ByVal strRutaFtp As String, ByVal strFileFtp As String, ByVal ftp_clave As String, ByVal ftp_usuario As String)

        Try
            Dim strDirFtp As FtpWebRequest = CType(FtpWebRequest.Create(strRutaFtp & strFileFtp), FtpWebRequest)
            strDirFtp.UsePassive = True
            Dim objNC As New NetworkCredential(ftp_usuario, ftp_clave)
            strDirFtp.Credentials = objNC
            strDirFtp.Method = WebRequestMethods.Ftp.DeleteFile
            strDirFtp.GetResponse()

        Catch ex As Exception
            'rp.Errores = True
            'rp.MensajeError = ex.Message
        End Try
    End Sub
End Class
